﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using IosDatamodel;
using System.Web.Mvc;
namespace IOSpro.Lib
{
    public class CommentsBL:ICommentsBL
    {
        private IosDBEntities db;
        private ModelStateDictionary _errg;

        public CommentsBL(ModelStateDictionary ModelState) 
        {
            db = new IosDBEntities();
            _errg = ModelState;
        }
        public CommentsBL()
        {
            db = new IosDBEntities();

        }
        //public int GetCommentCountByprojectSN(string projectSN) 
        //{
        //    return (from a in db.Feedbacks where a.ProjectSN == projectSN select a).Count();
        //    //db.Feedbacks.Select(a => a.ProjectSN == projectSN).Count();   
        //}
        //public decimal GetAverageStarvalueByprojectSN(string projectSN) 
        //{
        //    decimal averagestarvalue;
        //  int toalstarvalue=0;
        //  float  i=0;
        //  db.Feedbacks.Where(a=>a.ProjectSN==projectSN).ToList().ForEach(a=>
        //  {
        //      if (a.StarValue !=0) {
        //          toalstarvalue += Convert.ToInt16(a.StarValue);
        //          i++;
              
        //      };
        //  });
        //  if (toalstarvalue == 0)
        //  {
        //      decimal aa = 0;
        //      return aa;

        //  }
        //  else {
        //      averagestarvalue = Convert.ToDecimal(toalstarvalue / i);

        //      return decimal.Round(averagestarvalue, 1);     
          
        //  }

           
        //}

      // public bool HaveCommentByprojectSN(string projectSN)
      //   {
      //       if ((from a in db.Feedbacks where a.ProjectSN==projectSN select a).Count() >0) return true;
      //       else return false;
         
      //   }
      //public  void DeleteCommentByprojectSN(string projectSN) 
      // {

      //     db.Feedbacks.Where(a => a.ProjectSN == projectSN).ToList().ForEach(a =>
      //    {
      //        db.Feedbacks.Remove(a);
      //    });
      //     db.SaveChanges();
      // }
       
        public void AddComment(string email, string usercomment, int videoid) 
      {
          Comments addusercomment = new Comments 
          {
               Email=email,
                UserComment=usercomment,
                 VideoId=videoid,
                  CommentDate=DateTime.Now
          };
          db.Comments.Add(addusercomment);
          db.SaveChanges();
      
      }
        public IQueryable<Comments> GetcommentsList(int videoId)
      {
          return db.Comments.OrderByDescending(a => a.CommentDate).Where(b => b.VideoId == videoId);      
      }


        public IQueryable<UserReplys> GetReplysList(int  usercommentid)
        {
            return db.UserReplys.OrderByDescending(a => a.ReplyDate).Where(b => b.UserCommentId == usercommentid);              
        }

       public void AddReply(string email, int usercommentid, string reply) 
    {

          UserReplys userreply = new UserReplys 
          {
                Email=email,
                UserCommentId = usercommentid,
                 Reply=reply,
                 ReplyDate = DateTime.Now
          };
          db.UserReplys.Add(userreply);
          db.SaveChanges();
    
    
    }

   //  public IQueryable<Feedback> GetAllFeedBackList(string projectSN)
   //   {
   //       return db.Feedbacks.OrderByDescending(a => a.ID).Where(b => b.ProjectSN == projectSN);      
   //   }


        
   //public bool DeleteCommentByID(int ID)
   //{
   //try{
   //    db.Feedbacks.Remove(db.Feedbacks.Find(ID));
   //    db.SaveChanges();
   //    return true;
   //          }catch{
   //    return false;
   //          }
       
   
   
   //}


   // public Feedback GetCommentByID(int ID) 
   //  {

   //      return db.Feedbacks.Single(a=>a.ID==ID);
   
   //  }


    }
}